package controller

import (
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type FakeRouteController struct{}

func (that FakeRouteController) FakeList(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTblFakeList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that FakeRouteController) UpdateFake(ctx *fasthttp.RequestCtx) {
	param := model.TblFake{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("假线路游戏更新", ctx)
	err = model.UpdateOrDelTblFake(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that FakeRouteController) DeleteFake(ctx *fasthttp.RequestCtx) {
	param := model.TblFake{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("假线路游戏更新", ctx)
	err = model.UpdateOrDelTblFake(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that FakeRouteController) FakeRouteList(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRouteInfoParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTblFakeRouteInfoList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that FakeRouteController) UpdateFakeRoute(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRouteInfo{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("假游戏线路更新", ctx)
	err = model.UpdateOrDelTblFakeRouteInfo(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that FakeRouteController) DeleteFakeRoute(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRouteInfo{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("假游戏线路删除", ctx)
	err = model.UpdateOrDelTblFakeRouteInfo(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that FakeRouteController) FakeRtp(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRtpParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetTblFakeRtpList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that FakeRouteController) UpdateFakeRtp(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRtp{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("假游戏rtp配置更新", ctx)
	err = model.UpdateOrDelTblFakeRtp(param, ctx, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that FakeRouteController) DeleteFakeRtp(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRtp{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("假游戏rtp配置删除", ctx)
	err = model.UpdateOrDelTblFakeRtp(param, ctx, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that *FakeRouteController) MerchantFakeUpdate(ctx *fasthttp.RequestCtx) {
	param := model.TblFakeRtp{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("一键同步官方配置", ctx)

	err = model.MerchantFakeUpdate(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	model.SynRedis(ctx)
	model.GameFlushAll()
	helper.RetData(ctx, true, "succeed")
}

func (that FakeRouteController) MerchantFake(ctx *fasthttp.RequestCtx) {
	param := model.TblOperatorInfoFakeParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetTblOperatorInfoFakeList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that FakeRouteController) UpdateMerchantFake(ctx *fasthttp.RequestCtx) {
	param := model.TblOperatorInfoFake{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("渠道假游戏控制配置更新", ctx)
	err = model.UpdateOrDelTblOperatorInfoFake(param, ctx, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
